#include "mainscene.h"
#include "ui_mainscene.h"
#include <QPainter>
#include "mypushbutton.h"
#include <QDebug>
#include <QTimer>
#include <QSound>
MainScene::MainScene(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainScene)
{
    ui->setupUi(this);
    //设置固定大小
    setFixedSize(320,588);
    //设置图标
    setWindowIcon(QIcon(":/resource/Coin0001.png"));
    //设置标题
    setWindowTitle("主场景");
    //设置退出按钮
    connect(ui->actionQuit,&QAction::triggered,this,[this]()
    {
       this->close();
    });

    //开始按钮
    MyPushButton *startBtn=new MyPushButton(":/resource/MenuSceneStartButton.png");
    //设置依附对象
    startBtn->setParent(this);
    startBtn->move(this->width()*0.5-startBtn->width()*0.5,this->height()*0.7);
    //开始按钮音效
    QSound *startBtnSound=new QSound(":/resource/TapButtonSound.wav",this);


    //实例化关卡选择界面
    chooselevelscene=new ChooseLevelScene;

    connect(startBtn,&MyPushButton::clicked,[=]()
    {
       startBtnSound->play();
       qDebug()<<"点击开始按钮";
       //动画实现
       startBtn->move(this->width()*0.5-startBtn->width()*0.5,this->height()*0.7);//重置按钮位置，防止出现按钮下移的Bug
       startBtn->zoom_down();
       startBtn->zoom_up();
       //延时进入选择关卡界面实现
       QTimer::singleShot(500,this,[=]()
       {
           chooselevelscene->setGeometry(this->geometry());
           //自身隐藏
           this->hide();
           //显示关卡界面
           chooselevelscene->show();


       });
    });

    //监听选择关卡的返回按钮的信号
    connect(chooselevelscene,&ChooseLevelScene::chooseSceneBack,this,[=]()
    {
       this->setGeometry(chooselevelscene->geometry());
       chooselevelscene->hide();
       this->show();//重新显示主场景


    });
}


void MainScene::paintEvent(QPaintEvent *)
{
    QPainter painter(this);
    QPixmap pix;
    //背景图1
    pix.load(":/resource/PlayLevelSceneBg.png");
    painter.drawPixmap(0,0,this->width(),this->height(),pix);
    //背景图2
    pix.load(":/resource/Title.png");
    //缩放一下
    pix=pix.scaled(pix.width()*0.5,pix.height()*0.5);
    painter.drawPixmap(10,30,pix);
}

MainScene::~MainScene()
{
    delete ui;
}
